<?php

namespace App\Http\Requests;

use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SubsequentCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'patient_id'  => 'required|integer|exists:tn_user_base,id',
            'hospital_id' => 'required|integer|exists:tn_scene,id',
            'doctor_id'   => [
                'required', 'integer', 'min:1',
                Rule::exists('tn_user_base', 'id')->where(function (Builder $query) {
                    $query->where('hospital_id', $this->input('hospital_id'));
                }),
            ],
            'clinic_at'   => 'required|date_format:Y-m-d',
        ];
    }

    public function messages()
    {
        return [
            'doctor_id.required'        => '必须选在一个医生。',
            'doctor_id.exists'          => '该医院没有您选择的医生。',
            'clinic_at.date'            => '就诊日期格式不正确，例：1987-04-13。',
            'clinic_at.before_or_equal' => '就诊日期 必须要早于现在。',
        ];
    }
}
